package kouda.viewer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class Article {

    public File file;

    public long lastModified; // cache to avoid unnecessary file system lookup
    public String title;
    public String author;
    
    public String text = null; // load text if null on load()
    
    private boolean isNew = false;

    public Article(File parent, String title, String author) // new file
    {
        file = new File(parent, title + ".txt");
        // just path, not committed to file system yet
        
        lastModified = System.currentTimeMillis();
        this.title = title;
        this.author = author;
        
        isNew = true;
    }
    
    public Article(File file) // existing file
    {
        this.file = file;
        lastModified = file.lastModified();
        title = getTitle();
        author = getAuthor();
    }

    private long getLastModified()
    {
        return file.lastModified();
    }
    
    private String getTitle() // "filename" for "filename.txt"
    {
        String filename = file.getName();
        return filename.substring(0, filename.length() - 4);
    }

    private String getAuthor() // parent directory
    {
        return file.getParentFile().getName();
    }

    public void moveTo(String newTitle) throws IOException // move file
    {
        File newFile = new File(file.getParentFile(), newTitle + ".txt");
        FileUtils.moveFile(file, newFile);
        file = newFile;
        title = newTitle;
    }

    public void delete() throws IOException // delete file
    {
        file.delete();
        // this entry is gone
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm");

    public String toString() // this appears on the list
    {
        return sdf.format(new Date(lastModified)) + "   " + title + "   (" + author + ")";
    }
    
    public void save() throws IOException
    {
        OutputStream out = new FileOutputStream(file);
        if (text != null)
        {
            IOUtils.write(text, out, "SJIS");
            out.flush();
        }
        out.close();
        
        lastModified = getLastModified();
        isNew = false;
    }
    
    public void load() throws IOException // read whole at once
    {
        if (isNew) return;
        if (text != null) return;
        
        InputStream in = new FileInputStream(file);
        char[] data = IOUtils.toCharArray(in, "SJIS");
        in.close();
        text = new String(data);
    }

//    private void load() throws IOException // line by line
//    {
//        InputStream in = new FileInputStream(file);
//        List<String> lines = IOUtils.readLines(in);
//        in.close();
//
//        StringBuilder sb = new StringBuilder();
//        for (String line : lines)
//        {
//            sb.append(line);
//            sb.append("\r\n");
//        }
//        
//        text = sb.toString();
//    }
}
